package com.wangsanfei.ihg.wechat.controller;

import com.jfinal.aop.Clear;
import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Record;
import com.wangsanfei.base.BaseController;
import com.wangsanfei.config.ConstConfig;
import com.wangsanfei.ihg.wechat.interceptor.AuthInterceptor;
import com.wangsanfei.model.User;
import com.wangsanfei.util.MD5Util;
import com.wangsanfei.util.SendMailUtil;
import com.wangsanfei.util.StringUtils;
import com.wangsanfei.vo.BaseVo;

public class LoginController extends BaseController{

	@Clear(AuthInterceptor.class)
	@ActionKey("/manageLogin")
	public void manage_login(){
		
		String username=getPara("username");
		String password=getPara("password");
		
		User user=null;
		
		if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(password)){
			User userT=User.dao.getUserByUsername(username);
			if(userT!=null){
				String pass=userT.getStr("password");
				if(pass!=null&&password!=null){
					pass=pass.toUpperCase();
					password=password.toUpperCase();
					if(pass.equals(password)){
						user=userT;
					}
				}
			}
		}
		
		if(user==null){
			setAttr("msg", "账号或密码错误！");
			renderJsp("/manage/login.jsp");
		}else{
			user=User.dao.getUserByUsername(username);
			getSession().setAttribute(ConstConfig.SESSION_USER,user);
			getSession().setAttribute(ConstConfig.SESSION_USER_TYPE,"0");
			redirect("/manage");
		}
	}
	
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/manageLoginOut")
	public void manage_login_out(){
		String userType=(String)getSession().getAttribute(ConstConfig.SESSION_USER_TYPE);
		getSession().invalidate();
		if("0".equals(userType)){
			renderJsp("/manage/login.jsp");
		}
		
		renderJsp("/manage/login.jsp");
		
	}
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/wechat/login")
	public void loginUser(){
		String forword=getPara("forword");
		String examId=getPara("examId");
		if(StringUtils.isEmpty(forword)){
			forword=(String)getRequest().getAttribute("forword");
		}
		if(StringUtils.isEmpty(examId)){
			examId=(String)getRequest().getAttribute("examId");
		}
		setAttr("forword", forword);
		setAttr("examId", examId);
		setAttr("loginout", getPara("loginout"));
		setAttr("forwordWechatSupport", getPara("forwordWechatSupport"));
		
		renderJsp("/wechat/login.jsp");
		
	}
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/wechat/login/action")
	public void loginUseraction(){
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(false);
		
		String merLinId=getPara("merLinId");
		String password=getPara("password");
		
		if(StringUtils.isEmpty(merLinId)){
			baseVo.setMsg("Merlin ID不能为空！");
			renderJson(baseVo);
			return;
		}
		if(StringUtils.isEmpty(password)){
			baseVo.setMsg("密码不能为空！");
			renderJson(baseVo);
			return;
		}
		
		merLinId=merLinId.toUpperCase();
		
		Record user=User.dao.getUserByMerLinId(merLinId);
		if(user==null){
			baseVo.setMsg("该Merlin ID尚未注册，请先注册！");
			renderJson(baseVo);
			return;
		}
		
		user=User.dao.getUserByMerLinIdAndPassword(merLinId, password);
		if(user==null){
			baseVo.setMsg("Merlin ID或密码错误！");
			renderJson(baseVo);
			return;
		}
		
		//2016-08-30，每次要判断该MerlinId有没有存在与MerlinId表里面，如果不存在，说明该用户已经离职
		Record merlinId=User.dao.getMerLinId(merLinId);
		if(merlinId==null){
			baseVo.setMsg("对不起，您已经没有权限登录本系统，如有问题请联系管理员！");
			renderJson(baseVo);
			return;
		}
		
		getSession().setAttribute(ConstConfig.SESSION_USER, user);
		getSession().setAttribute(ConstConfig.SESSION_USER_TYPE, "2");
		
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/wechat/email/sendpassword/merLinId")
	public void wechat_email_sendpasswordmerLinId(){
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(false);
		
		String merLinId=getPara("merLinId");
		
		if(StringUtils.isEmpty(merLinId)){
			baseVo.setMsg("Merlin ID不能为空！");
			renderJson(baseVo);
			return;
		}
		
		merLinId=merLinId.toUpperCase();
		
		Record user=User.dao.getUserByMerLinId(merLinId);
		if(user==null){
			baseVo.setMsg("该Merlin ID尚未注册，请先注册！");
			renderJson(baseVo);
			return;
		}
		
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/wechat/email/sendpassword")
	public void wechat_email_sendpassword(){
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(false);
		
		String merLinId=getPara("merLinId");
		
		if(StringUtils.isEmpty(merLinId)){
			baseVo.setMsg("Merlin ID不能为空！");
			renderJson(baseVo);
			return;
		}
		
		merLinId=merLinId.toUpperCase();
		
		Record user=User.dao.getUserByMerLinId(merLinId);
		if(user==null){
			baseVo.setMsg("该Merlin ID尚未注册，请先注册！");
			renderJson(baseVo);
			return;
		}
		
		SendMailUtil.sendMailExchange("洲际酒店找回密码", "洲际酒店提醒：<br/><br/>您的登录密码是："+user.getStr("password"), user.getStr("email"));
		
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/loginout")
	public void loginout(){
		
		getSession().invalidate();
		String forwordWechatSupport=getPara("forwordWechatSupport");
		redirect("/wechat/login?loginout=true&forwordWechatSupport="+forwordWechatSupport);
		
	}
	
	@Clear(AuthInterceptor.class)
	@ActionKey("/wechat/login/cookie/action")
	public void loginUserCookieAction(){
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(false);
		
		String merLinId=getPara("merLinId");
		
		if(StringUtils.isEmpty(merLinId)){
			baseVo.setMsg("Merlin ID不能为空！");
			renderJson(baseVo);
			return;
		}
		
		merLinId=merLinId.toUpperCase();
		
		//2016-08-30，每次要判断该MerlinId有没有存在与MerlinId表里面，如果不存在，说明该用户已经离职
		Record merlinId=User.dao.getMerLinId(merLinId);
		if(merlinId==null){
			baseVo.setMsg("对不起，您已经没有权限登录本系统，如有问题请联系管理员！");
			renderJson(baseVo);
			return;
		}
		
		Record user=User.dao.getUserByMerLinId(merLinId);
		if(user==null){
			baseVo.setMsg("该Merlin ID尚未注册，请先注册！");
			renderJson(baseVo);
			return;
		}
		
		getSession().setAttribute(ConstConfig.SESSION_USER, user);
		getSession().setAttribute(ConstConfig.SESSION_USER_TYPE, "2");
		
		baseVo.setFlag(true);
		renderJson(baseVo);
	}
	
	public static void main(String[] args) {
		System.out.println(MD5Util.MD5("dashihg2015"));
	}
}
